home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
BARNET
/
FREENET
/
BRODIE
/
INTERNET
/
!InternetD
/
c
/
echo
< prev
next >
Wrap
Text File
|
1995-06-06
|
2KB
|
66 lines
/* echo.c - the echod for !InternetD */
#include "inetd.h"
#define ECHOSIZE 256
void echo_udp(int s)
{
struct sockaddr_in address;
int size = sizeof(struct sockaddr_in);
char echobuff[ECHOSIZE];
int length;
address.sin_addr.s_addr = INADDR_ANY;
length = recvfrom(s, echobuff, ECHOSIZE, 0, (struct sockaddr *)&address, &size);
if (length > 0) {
if (ioctl(s, FIONBIO, (char *)1) == -1) {
syslog(0, "ioctl() failed for echo/udp");
}
sendto(s, echobuff, length, 0, (struct sockaddr *)&address, size);
if (ioctl(s, FIONBIO, NULL) == -1) {
syslog(0, "ioctl() failed for echo/udp");
}
}
}
void echo_tcp(int s)
{
int t;
struct sockaddr_in address;
int size = sizeof(struct sockaddr_in);
inet_handler *ih;
t = accept(s, (struct sockaddr *)&address, &size);
if (t == -1) return;
ih = worker_register(t, echo_data, worker_io_read);
if (ih) {
if (ioctl(t, FIONBIO, (char *)1) == -1) {
syslog(0, "ioctl() failed for echo/tcp");
}
syslog(0, "established echo/tcp connection");
}
else {
kill_socket(&t);
}
}
void echo_data(inet_handler *ih, worker_io_flags f)
{
char data[ECHOSIZE];
int result;
UNUSED(f);
result = recv(ih->data_socket, data, ECHOSIZE, 0);
if (result == -1 && errno == EWOULDBLOCK) return;
if (result < 1) {
worker_deregister(ih->data_socket);
return;
}
result = send(ih->data_socket, data, result, 0);
if (result < 0 && errno != EWOULDBLOCK) {
worker_deregister(ih->data_socket);
}
}